package LogServer.utils

import LogServer.constants.PropertiesNodejs

import scalaj.http.{Http, HttpOptions}

/**
  * Created by root on 17-2-23.
  */

class HttpRequest{
  /**
    * 发送Http Post请求
    * @param url 请求地址
    * @param data 请求参数
    * @return
    */
  def post(url: String, data: String): String ={
    val response = Http(url).postData(data)
      .header("Content-Type", "application/json")
      .header("Charset", "UTF-8")
      .option(HttpOptions.readTimeout(10000)).asString
    var result = ""
    if(response.isSuccess){
      result = response.body
    }
    result
  }
}

object HttpRequest{
  def main(args: Array[String]) {
    val request = new HttpRequest()
    val s = s"""{"time":"2017-03-01 18:41:34","host_mem":"70254592","host_cpu":"85","vm_mem":"50331648","host_id":"69","vm_cpu_num":"16","vm_id":"1","vm_cpu":"46","vm_cpu_rate":"2"}"""
    val d1 = "test"
    val data = s"""{"test111":"$d1"}"""
    println(s)
    val ip = PropertiesNodejs.getUrl()
    val result = request.post("http://"+ip+"/log/vmhost",s)
    println(result)
  }
}

